package yxy.game.pm2.command.hero;

import org.redisson.api.RLock;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.HeroMessage;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.resonate.ResonateHandle;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@MessageType(proto = Command.HeroLevelUp, classtype = HeroMessage.HeroLevelUpRequest.class)
public class CMD_HeroLevelUp extends Command<HeroMessage.HeroLevelUpRequest> {

    @Override
    public void execute(MessageSource messageSource, HeroMessage.HeroLevelUpRequest request) {
        Date date = new Date();
        String playerCode = messageSource.getTag();
        String code = request.getCode();
        int times = request.getTimes();

        HeroMessage.HeroLevelUpResponse.Builder response = HeroMessage.HeroLevelUpResponse.newBuilder();

        RLock lock = Hero.getRedisLock(Hero.lockKey(playerCode));
        try {
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
                //todo  业务处理
                HeroHandle heroHandle = new HeroHandle();
                Hero hero = heroHandle.getHero(playerCode, code);
                if (hero == null || times < 1) {
                    send(messageSource, response, Result.ParamError);
                    return;
                }

                //共鸣的武将不能升级
                Result resultForResonate = ResonateHandle.heroIsResonateHero(hero, playerCode);
                if (!resultForResonate.equals(Result.Success)) {
                    send(messageSource, response, resultForResonate);
                    return;
                }


                Result result = heroHandle.levelUp(playerCode, hero, times);

                if (!result.equals(Result.Success)) {
                    send(messageSource, response, result);
                    return;
                }

                HeroMessage.HeroInfo heroInfo = heroHandle.getHeroInfo(playerCode, hero);
                response.setInfo(heroInfo);
                send(messageSource, response, result);

                //判断如果是共鸣系统场上的武将，升级一系列操作
                if (result.equals(Result.Success)) {
                    ResonateHandle.heroIsAboveHeroAndPromote(hero, playerCode);
                }
            } else {
                send(messageSource, response, Result.Success);
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }

        Date date1 = new Date();
        System.out.println("CMD_HeroLevelUp:" + (date1.getTime() - date.getTime()));
    }

    private void send(MessageSource messageSource, HeroMessage.HeroLevelUpResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }

}
